#include "TerminoSimplificado.h"
#include <iostream>
#include <stdio.h>
#include <string.h>



TerminoSimplificado::TerminoSimplificado(Termino_Parseado* ter) {
	memcpy(&this->ter,ter,sizeof(Termino_Parseado));
	this->tamanio = sizeof(Termino_Parseado);
	this->cant_doc = 0;
}

TerminoSimplificado::~TerminoSimplificado() {
	std::list<Doc_frec*>::iterator it = this->duplas.begin();
	while (it != this->duplas.end()) {
		delete (*it);
		it++;
	}
}

void TerminoSimplificado::agregar_doc(unsigned short int doc, unsigned short int frec) {
	Doc_frec* par = new Doc_frec;
	par->doc = doc;
	par->frec = frec;
	this->duplas.push_back(par);
	this->cant_doc++;
	this->tamanio = this->tamanio +   sizeof(unsigned short int)*2;
}

void TerminoSimplificado::grabar(char* buffer, int* pos_actual) {
	memcpy(&buffer[(*pos_actual)],  &this->ter.termino, 40 );
	(*pos_actual) = (*pos_actual) + 40;
	memcpy(&buffer[(*pos_actual)], &this->cant_doc, sizeof(unsigned short int));
	(*pos_actual) = (*pos_actual) + sizeof(unsigned short int);
	unsigned short int doc = 0;
	unsigned short int frec = 0;
	std::list<Doc_frec*>::iterator it = this->duplas.begin();
	while (it != this->duplas.end()) {
		doc = (*it)->doc;
		frec = (*it)->frec;
		memcpy(&buffer[(*pos_actual)], &doc, sizeof(unsigned short int));
		(*pos_actual) = (*pos_actual) + sizeof(unsigned short int);
		memcpy(&buffer[(*pos_actual)], &frec, sizeof(unsigned short int));
		(*pos_actual) = (*pos_actual) + sizeof(unsigned short int);
 		it++;
	}
}

int TerminoSimplificado::size() {
	return this->tamanio;
}
